import React from 'react';
import router from 'umi/router';
//import Link from 'umi/link'

import { connect } from 'dva';

import { Card, // Divider,
  Button, //Table,
  Modal
} from 'antd';

import DescriptionList from '@/components/DescriptionList';
const { Description } = DescriptionList;

const formPath = '/AccountPayment/PaymentForm';
const listPath = '/AccountPayment/Payments';


@connect(({ accountPayment }) => ({ accountPayment }))
class Profile extends React.Component {
  state = {

    visibleDel: false,
    delMessage: '',
    visiblePost: false,
    visibleUnpost: false,

  }

  id = 0

  async componentDidMount() {
    const { location: {query: { id } } } = this.props;
    this.id = Number(id)
    if (this.id) {
      const { dispatch } = this.props;
      dispatch({
        type: 'accountPayment/view',
        payload:  { id: this.id }
      });
    }
  }


  goto_createForm() {
    const { accountPayment: { record } } = this.props;
    if(record.id){
      router.push(`${formPath}?id=${record.id}`);
    }
  }

  showDeleteMove() {
    this.setState({
      visibleDel: true
    })

  }

  showPostMove() {
    this.setState({
      visiblePost: true
    })

  }

  handleCancel(){
    this.setState({
      visibleDel: false,
      visiblePost: false,
      visibleUnpost: false,
    })
  }

  async delMove(){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'accountPayment/unlink',
        payload:  { id: this.id }
      });

      const { accountPayment } = this.props;
      if( accountPayment.id ){
        this.setState({
          delMessage: '不能删除该记录'
        })
      }
      else{
        router.push(listPath);

      }
    }
  }



  async postMove(){

    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'accountPayment/post',
        payload:  { id:this.id }
      });

      this.setState({
        visiblePost: false
      })

    }
  }



  render() {
    const { accountPayment: { record } } = this.props;

    const { visiblePost, //visibleUnpost,
      visibleDel,delMessage } = this.state

    return (
      <Card bordered={false}>
        <div size="large" title='说明' style={{ marginBottom: 32 }}>
          <div>拆分为 收款单 / 付款单 / 内部转款单</div>
        </div>

        <DescriptionList size="large" title='操作控制' style={{ marginBottom: 32 }}>
          <Description >
            <Button onClick={()=>this.goto_createForm()}>编辑</Button>
          </Description>
          <Description >
            <Button onClick={()=>this.showDeleteMove()}>删除</Button>
          </Description>

          <Description >
            <Button onClick={()=>this.showPostMove()}>过账</Button>
          </Description>

        </DescriptionList>

        <Modal title='删除'
          visible={visibleDel}
          onOk={()=>this.delMove()}
          onCancel={()=>this.handleCancel()}
        >
          确认删除?
          {delMessage}

        </Modal>

        <Modal title='过账'
          visible={visiblePost}
          onOk={()=>this.postMove()}
          onCancel={()=>this.handleCancel()}
        >
          确认过账?
        </Modal>


        <DescriptionList size="large" title='概要信息' style={{ marginBottom: 32 }}>
          <Description term='支付类型' >{record.payment_type}</Description>
          <Description term='业务伙伴类型' >{record.partner_type}</Description>
          <Description term='状态' >{record.state}</Description>
          <Description term='日期' >{record.payment_date}</Description>
          <Description term='业务伙伴' >{record.partner_id && record.partner_id.name}</Description>
          <Description term='总金额' >{record.amount}</Description>
          <Description term='支付方式' >{record.payment_method_id && record.payment_method_id.name}</Description>
          <Description term='账簿' >{record.journal_id && record.journal_id.name}</Description>
          <Description term='目标账簿' >{record.destination_journal_id && record.destination_journal_id.name}</Description>

        </DescriptionList>
        <DescriptionList size="large" title='会计信息' style={{ marginBottom: 32 }}>
          <Description term='名称' >{record.name}</Description>
        </DescriptionList>
        <DescriptionList size="large" title='其他信息' style={{ marginBottom: 32 }}>
        </DescriptionList>
      </Card>
    );
  }
}

//

export default Profile;

